package com.app.model 
{
	import flash.events.Event;
	import flash.events.DataEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	import org.puremvc.as3.interfaces.IProxy;
	
	import com.app.AppFacade;
	
	import com.peachcastle.socket.PClient;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class HallConnProxy extends Proxy implements IProxy
	{
		public static const NAME:String = 'HallConnProxy';
		
		private static const INT_TYPE:Array = ['count'];
		
		private var _client:PClient = null;
		
		private var _isConn:Boolean = false;
		
		private var _receiveCmd:int = 0;
		
		public function HallConnProxy() 
		{
			super(NAME);
		}

		public function connect(ip:String, port:Number):void
		{
			Log.info("hall connect... ip = " + ip + " port = " + port);
			
			var p : Object = {
				protocol		: 'Muduo', 
				addr			: ip, 
				port			: port, 
				on_connect		: hallOnConnect, 
				on_close		: hallOnClose, 
				on_cmd			: hallOnCmd
			};
			
			_client = new PClient(p);
		}
		
		public function close():void
		{
			if (_isConn)
			{
				_client.socketClose();
				_isConn = false;
			}
		}
		
		private function hallOnConnect():void
		{
			_isConn = true;
			
			sendNotification(AppFacade.LOGIN_HALL);
		}
		
		public function sendData(cmd:String, data:Object = null):void
		{
			if (!_isConn)
			{
				sendNotification(AppFacade.CLIENT_NOTICE, ['error_100']);
				return;
			}
			
			switch(cmd)
			{
				case 'login':
					data["cmd_id"] = CMD_LOGIN_HALL_REQ;
					
					break;
				
				case "create_table":
					data["cmd_id"] = CMD_CREATE_ROOM_REQ;

					break;
					
				case 'quick_join_start_game':
					data["cmd_id"] = CMD_HALL_START_GAME_REQ;
				
					break;
			}
			
			var len:int = _client.send(data);
			
			Log.info("hall send data ====== : cmd = " + cmd, data);
			
		}
		
		private function hallOnCmd(cmd:Object):void
		{
			_receiveCmd = cmd.params.cmd_id;
			
			if (cmd.params['err'] > 0)
			{
				sendNotification(AppFacade.SERVER_NOTICE, [cmd.params['err']]);
				
				return;
			}
			
			if (cmd.params.hasOwnProperty('err')) delete cmd.params['err'];
			if (cmd.params.hasOwnProperty('msg')) delete cmd.params['msg'];
			if (cmd.params.hasOwnProperty('cmd_id')) delete cmd.params['cmd_id'];
			
			for (var key:String in cmd.params)
			{
				if (INT_TYPE.indexOf(key) != -1 && typeof(cmd.params[key]) == 'string')
				{
					cmd.params[key] = int(cmd.params[key]);
				}
			}
			
			Log.info("hall receve data ====== : cmd = " + _receiveCmd, cmd.params);
			
			switch (int(_receiveCmd))
			{
				case CMD_LOGIN_HALL_RES:
					loginSuccess(cmd.params);
					
					break;
					
				case CMD_CREATE_ROOM_RES:
					addNewRoom(cmd.params);
					
					break;
					
				case CMD_ROOM_UPDATE_RES:
					updateRoom(cmd.params);
					
					break;
					
				case CMD_HALL_START_GAME_RES:
					joinRoom(cmd.params);
					
					break;
			}
		}
		
		private function loginSuccess(data:Object):void
		{
			sendNotification(AppFacade.ENTER_HALL, [data]);	//data是房间列表
		}
		
		private function addNewRoom(data:Object):void
		{
			sendNotification(AppFacade.ADD_NEW_ROOM, [data]);
		}
		
		private function updateRoom(data:Object):void
		{
			sendNotification(AppFacade.UPDATE_ROOM, [data]);
		}
		
		private function joinRoom(data:Object):void
		{
			sendNotification(AppFacade.HALL_START_GAME, [data]);
		}
		
		public function hallOnClose():void
		{
			_isConn = false;
			sendNotification(AppFacade.CLIENT_NOTICE, ['error_100']);
			Log.info("hallOnClose...");
		}
		
		private function ioErrorHandler(e:IOErrorEvent):void
		{
			_isConn = false;
			sendNotification(AppFacade.CLIENT_NOTICE, ['error_100']);	
			Log.info("Hall IOErrorEvent...");
		}
		
		private function securityErrorHandler(e:SecurityErrorEvent):void
		{
			_isConn = false;
			sendNotification(AppFacade.CLIENT_NOTICE, ['error_100']);	
			Log.info("Hall SecurityErrorEvent...");
		}
	}

}
include "./socket/CMD.as"